package net.cjw.initsb.controller.background;

import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresGuest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Strings;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiParam;
import net.cjw.initsb.common.ServiceResult;
import net.cjw.initsb.common.ServiceResult.ServiceResultMsg;
import net.cjw.initsb.entity.ErrorLog;
import net.cjw.initsb.service.ErrorLogServiceI;
import net.cjw.initsb.utils.UUIDBuild;

@Api(description="错误日志")
@RestController
@RequestMapping("error_log")
public class ErrorLogController {

	Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private ErrorLogServiceI errorLogServiceI;

	@PostMapping("v{version}/create")
	public ServiceResult<String> create(
			@ApiParam(value = "版本号", defaultValue = "1") @PathVariable("version") Integer version,
			@ApiParam(value = "日志生成地方", defaultValue = "后台服务器") @RequestParam String create_addr,
			@ApiParam(value = "日志内容", defaultValue = "我是日志内容") @RequestParam String log_content) {

		if (Strings.isNullOrEmpty(create_addr) || Strings.isNullOrEmpty(log_content)) {
			return ServiceResult.buildError(ServiceResultMsg.REQUEST_PARAM_ERROR_MSG);
		}
		if (version == 1) {
			ErrorLog el = new ErrorLog();
			el.setId(UUIDBuild.createUUId());
			el.setCreateAddr(create_addr);
			el.setLogContent(log_content);
			el.setCreateTime(new Date());
			return errorLogServiceI.create(el);
		}
		return ServiceResult.buildError(ServiceResultMsg.VERSION_ERROR_MSG);
	}

}
